package com.treemanage.treemanageback.controller;

import com.treemanage.treemanageback.JWTest;
import com.treemanage.treemanageback.pojo.Result;
import com.treemanage.treemanageback.pojo.User;
import com.treemanage.treemanageback.service.LoginService;
import com.treemanage.treemanageback.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录and注册控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@Validated
@Slf4j
public class LoginController {
    @Autowired
    private LoginService userService;

    /**
     * 注册接口
     *
     * @param username 用户名称
     * @param password 用户密码
     * @param phone    手机
     * @param email    邮箱
     * @return Result
     */
    @PostMapping("/register")
    public Result result(@Pattern(regexp = "^[a-zA-Z0-9_-]{4,16}$", message = "用户名格式错误") String username,
                         @Pattern(regexp = "^[a-zA-Z0-9_-]{6,18}$", message = "密码格式错误") String password,
                         @Pattern(regexp = "^1[3456789]\\d{9}$", message = "手机号格式错误")
                         String phone, String email) {
        log.info("注册: {}, {}", username, password);
        User u = userService.findUserByUsername(username);
        if (u == null) {
            userService.add(username, password, phone, email);
            return Result.success("注册成功");
        } else {
            return Result.error("用户名已存在");
        }

    }

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return Result
     */

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^[a-zA-Z0-9_-]{4,16}$", message = "用户名格式错误") String username,
                        String password) {
        log.info("登录{}, {}", username, password);
        User u = userService.findUserByUsername(username);
        if (u != null && u.getPassword().equals(password)) {
            Map<String, Object> cliam = new HashMap<>();
            // 获取token
            String token = JWTest.genToke(cliam);
            // 设置返回值
            cliam.put("id", u.getId());
            cliam.put("username", u.getUsername());
            cliam.put("phone", u.getPhone());
            cliam.put("email", u.getEmail());
            cliam.put("createTime", u.getCreat_time());
            cliam.put("updateTime", u.getUpdate_time());
            cliam.put("token", token);
            return Result.success(cliam);
        } else if (u == null) {
            return Result.error("用户名不存在");
        } else {
            return Result.error("密码错误");
        }
    }
}
